/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onpu_dms_xls.kernel.common.XML_Parsers;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import onpu_dms_xls.kernel.common.inputParameters.FromDB_InputParameters;
import onpu_dms_xls.kernel.common.inputParameters.Variable;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.Logger;

/**
 *
 * @author snbm
 */
public class SAX_XML_FromDB_InputParam_Parser extends DefaultHandler {
    

    private String tempVal;
    //to maintain context
    FromDB_InputParameters fDBip;
    Variable tempVar;

    public void runExample(String xmlFilePath, FromDB_InputParameters fDBip) throws IOException{
        this.fDBip = fDBip;
        LoggingFacade.trace.debug("in runExample()");
        parseDocument(xmlFilePath);
        return;
    }


    private void parseDocument(String xmlFilePath) throws IOException{
        LoggingFacade.trace.debug("in parseDocument()");
        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            LoggingFacade.trace.debug("got a Factory");
            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();
            LoggingFacade.trace.debug("got a parser, starting the parsing...");
            //parse the file and also register this class for call backs
            sp.parse(xmlFilePath, this);

        } catch (SAXException se) {
            throw new IOException(se.getMessage());
        } catch (ParserConfigurationException pce) {
            throw new IOException(pce.getMessage());
        } 
    }

    //Event Handlers
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //reset
        LoggingFacade.trace.debug("in startElement, qName: "+qName);
        tempVal = "";

        if (qName.equalsIgnoreCase("param")){
            LoggingFacade.trace.debug("still in startElement, variable:"+attributes.getValue("name"));
            this.tempVar = new Variable(attributes.getValue("name"), "");
        }

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch, start, length);
        LoggingFacade.trace.debug("in characters(), value: "+tempVal);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        LoggingFacade.trace.debug("in endElements, qName: "+qName);
        if (qName.equals("outputFolder")){
            this.fDBip.setConstantParam_outputFolder(tempVal);
        } else if (qName.equals("output_file_name")){
            this.fDBip.setConstantParam_outputFileName(tempVal);
        } else if (qName.equals("singleFromDBQuery")){
            this.fDBip.setConstantParam_singleFromDBQuery(tempVal);
        } else if (qName.equals("temporaryFolder")){
            this.fDBip.setConstantParam_temporaryFolder(tempVal);
        } else if (qName.equals("param")){
            this.tempVar.setValue(tempVal);
            this.fDBip.getOtherVars().add(this.tempVar);
        } else if (qName.equals("documentClassId")){
            this.fDBip.setDocumentClassId(Integer.parseInt(tempVal));
        } 
    }
}